﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace socialnetworkApp.Core.GlobalVariableClass
{
    public static  class  decodeJwtToken
    {

        public static  User   Decode(string Token) {

            string[] segments = Token.Split('.');
            string header = segments[0];
            string payload = segments[1];
            string sig = segments[2];
            string json = JwtDecode(payload);
           return jsonStrConvent(json);
        }

        //解析token
     public static   string JwtDecode(string s)
        {
            s = s.Replace('-', '+').Replace('_', '/');
            switch (s.Length % 4)
            {
                case 2:
                    s += "==";
                    break;
                case 3:
                    s += "=";
                    break;
            }
            var bytes = Convert.FromBase64String(s);
            return Encoding.UTF8.GetString(bytes);
        }


      private static  User jsonStrConvent(string json)
        {

            User user = JsonConvert.DeserializeObject<User>(json);

            return user;
        }
    }
    public class User
    {

        public string? EmailAddress { get; set; }


        public string? Password { get; set; }


        public string? Uid { get; set; }


        public string? Role { get; set; }


        public long Exp { get; set; }


        public string? Iss { get; set; }


        public string? Aud { get; set; }
    }
}
